package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Arrays;

import lombok.AllArgsConstructor;

import javax.validation.constraints.*;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.business.vo.HcxAppVersionVo;
import com.ruoyi.business.bo.HcxAppVersionQueryBo;
import com.ruoyi.business.bo.HcxAppVersionAddBo;
import com.ruoyi.business.bo.HcxAppVersionEditBo;
import com.ruoyi.business.service.IHcxAppVersionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * app版本管理Controller
 *
 * @author wsdz
 * @date 2022-09-01
 */
@Api(value = "app版本管理控制器", tags = {"app版本管理管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/AppVersion")
public class HcxAppVersionController extends BaseController {

    private final IHcxAppVersionService iHcxAppVersionService;

    /**
     * 查询app版本管理列表
     */
    @ApiOperation("查询app版本管理列表")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxAppVersionVo> list(@Validated HcxAppVersionQueryBo bo) {
        return iHcxAppVersionService.queryPageList(bo);
    }

    /**
     * 导出app版本管理列表
     */
    @ApiOperation("导出app版本管理列表")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:export')")
    @Log(title = "app版本管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxAppVersionQueryBo bo) {
        List<HcxAppVersionVo> list = iHcxAppVersionService.queryList(bo);
        ExcelUtil<HcxAppVersionVo> util = new ExcelUtil<>(HcxAppVersionVo.class);
        return util.exportExcel(list, "app版本管理");
    }

    /**
     * 获取app版本管理详细信息
     */
    @ApiOperation("获取app版本管理详细信息")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxAppVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxAppVersionService.queryById(id));
    }

    /**
     * 新增app版本管理
     */
    @ApiOperation("新增app版本管理")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:add')")
    @Log(title = "app版本管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxAppVersionAddBo bo) {
        return toAjax(iHcxAppVersionService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改app版本管理
     */
    @ApiOperation("修改app版本管理")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:edit')")
    @Log(title = "app版本管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxAppVersionEditBo bo) {
        return toAjax(iHcxAppVersionService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除app版本管理
     */
    @ApiOperation("删除app版本管理")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:remove')")
    @Log(title = "app版本管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxAppVersionService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入app版本管理数据
     */
    @ApiOperation("导入app版本管理数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:AppVersion:import')")
    @Log(title = "app版本管理" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxAppVersionVo> util = new ExcelUtil<>(HcxAppVersionVo.class);
        List<HcxAppVersionVo> hcxAppVersionList;
        try {
            hcxAppVersionList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxAppVersionService.importHcxAppVersion(hcxAppVersionList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxAppVersionVo> util = new ExcelUtil<>(HcxAppVersionVo.class);
        return util.importTemplateExcel("app版本管理数据");
    }




}
